﻿
using OPCTreeMonitor.SQLdb.dbModel;
using System.Windows;



namespace OPCTreeMonitor.InterfaceWindow
{
    /// <summary>
    /// InterfaceAddSingle.xaml 的交互逻辑
    /// </summary>
    public partial class InterfaceAddSingle : Window
    {
        public event Action<OPCMonitorNode> NodeAdded;
        private string parentPath;

        public InterfaceAddSingle( string parentPath)
        {
            InitializeComponent();
            Title = $"在 {parentPath} 下添加节点";
            tb_Type.ItemsSource = cBXType.GetNames(typeof(cBXType));
            this.parentPath = parentPath;
            tb_GroupName.Text = parentPath; // 设置 tb_GroupName 的文本为当前组名
            btn_Ensure.Click += Btn_Ensure_Click;
        }
        private enum cBXType
        {
            Bool,
            Int,
            Float,
            Double,
            String,

        }
        private void Btn_Ensure_Click(object sender, RoutedEventArgs e)
        {
            string nodeName = tb_NodeName.Text;
            string nodePreSetValue = tb_NodePreSetValue.Text;
            string type = tb_Type.Text;
            string nodeDescription = tb_NodeDescription.Text;
            string nodeWrite = tb_NodeWrite.Text;
            string nodeRead = tb_NodeRead.Text;

            if (string.IsNullOrEmpty(nodeName))
            {
                MessageBox.Show("节点名称不能为空。");
                return;
            }

            OPCMonitorNode newNode = new OPCMonitorNode
            {
                GroupName = parentPath,
                NodeName = nodeName,
                NodePreSetValue = nodePreSetValue,
                Type = type,
                NodeDescription = nodeDescription,
                NodeWrite = nodeWrite,
                NodeRead = nodeRead,

            };

            NodeAdded?.Invoke(newNode);
            
            this.Close();
        }
    }
}
